﻿//Class: Phân quyền nhóm 
//Người tạo:		Ngày tạo:	
//Mô tả: Quản lý nhóm người dùng.
//Các Method cần thiết : 
//		- ListByAll() : Trả về danh sách tất các các nhóm người dùng trong hệ thống.
//			Return: DataSet.
//		- ListByID(int intID) : Trả về thông tin nhóm người dùng theo ID.
//			Return: Object NhomNguoiDung.
//		- ListUserByIDNhomNguoiDung(int intIDNhomNguoiDung) : Trả về danh sách các người dùng thuộc nhóm.
//			Return: Dataset.
//		- DoInsert(string strTenNhom, string strMoTa): Insert một nhóm vào cơ sở dữ liệu.
//			Return: int (Nếu gặp lỗi return -1, ngược lại return ID của nhóm mới Insert).
//		- DoUpdate(int intIDNhomNguoiDung, string strTenNhom, string strMoTa) : Cập nhật thông tin của nhóm vào cơ sở dữ liệu.
//			Return: int (Nếu gặp lỗi return -1).
//		- DoDelete(int intIDNhomNguoiDung): Xóa thông tin của nhóm người dùng.
//			Return: int (Nếu gặp lỗi return -1).
using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

using QLNHK.BLL;

namespace QLNHK.BLL {
	public class Cls_PHANQUYEN_NHOM
	{
		private const string ST_PHANQUYEN_NHOM_INSERT = "st_PHANQUYEN_NHOMInsert";
		private const string ST_PHANQUYEN_NHOM_UPDATE = "st_PHANQUYEN_NHOMUpdate";
		private const string ST_PHANQUYEN_NHOM_DELETE = "st_PHANQUYEN_NHOMDeleteByID_DS_ChucNang";
		private const string ST_PHANQUYEN_NHOM_DELETE_BY_IDCHUCNANG = "st_PHANQUYEN_NHOMDeleteByID_DS_ChucNang";
		private const string ST_PHANQUYEN_NHOM_DELETE_BY_ID_PHANQUYEN_NHOM = "st_PHANQUYEN_NHOMDeleteByID_PhanQuyen_Nhom";
		private const string ST_PHANQUYEN_NHOM_SELECTALL = "st_PHANQUYEN_NHOMSelectALL";
		private const string ST_PHANQUYEN_NHOM_SELECT_BY_IDNHOM = "st_PHANQUYEN_NHOMSelectByIDNhom";
		private const string ST_PHANQUYEN_NHOM_SELECT_ROLE_BY_IDCHUCNANG = "st_PHANQUYEN_NHOM_SELECT_ROLE_BY_IDCHUCNANG";
		private const string TABLE_NAME = "tb_PHANQUYEN_NHOM";
		private Database db;
		private int _iD_PhanQuyen_Nhom;
		private int _iD_Nhom_NguoiDung;
		private int _iD_DS_ChucNang;
		private string _maTruyCap;
		public int ID_PhanQuyen_Nhom
		{
			get{return this._iD_PhanQuyen_Nhom;}
			set{this._iD_PhanQuyen_Nhom = value;}
		}
		public int ID_Nhom_NguoiDung
		{
			get{return this._iD_Nhom_NguoiDung;}
			set{this._iD_Nhom_NguoiDung = value;}
		}
		public int ID_DS_ChucNang
		{
			get{return this._iD_DS_ChucNang;}
			set{this._iD_DS_ChucNang = value;}
		}
		public string MaTruyCap
		{
			get{return this._maTruyCap;}
			set{this._maTruyCap = value;}
		}
		public Cls_PHANQUYEN_NHOM()
		{
			try
			{
				db = DatabaseFactory.CreateDatabase();
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}

		public Cls_PHANQUYEN_NHOM(int id_phanquyen_nhom, int id_nhom_nguoidung, int id_ds_chucnang, string matruycap)
		{
			this._iD_PhanQuyen_Nhom = id_phanquyen_nhom;
			this._iD_Nhom_NguoiDung = id_nhom_nguoidung;
			this._iD_DS_ChucNang = id_ds_chucnang;
			this._maTruyCap = matruycap;
		}
		/// Author: khanhlq . Date: 2/8/2006.
		/// Modifier: khanhlq. Date:  2/8/2006.
		/// Liệt kê những gì đã chỉnh sửa:
		/// <summary>
		/// Inserts the wrapper.
		/// </summary>
		/// <returns>dbCommand</returns>
		private DbCommand InsertWrapper()
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_INSERT);
			db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32,"ID_Nhom_NguoiDung", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@ID_DS_ChucNang", DbType.Int32,"ID_DS_ChucNang", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@MaTruyCap", DbType.String,"MaTruyCap", DataRowVersion.Current);
			return dbCommand;
		}
		/// Author: khanhlq . Date: 2/8/2006.
		/// Modifier: khanhlq. Date:  2/8/2006.
		/// Liệt kê những gì đã chỉnh sửa:
		/// <summary>
		/// Updates the wrapper.
		/// </summary>
		/// <returns></returns>
		private DbCommand UpdateWrapper()
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_UPDATE);
			db.AddInParameter(dbCommand,"@ID_PhanQuyen_Nhom", DbType.Int32,"ID_PhanQuyen_Nhom", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32,"ID_Nhom_NguoiDung", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@ID_DS_ChucNang", DbType.Int32,"ID_DS_ChucNang", DataRowVersion.Current);
			db.AddInParameter(dbCommand,"@MaTruyCap", DbType.String,"MaTruyCap", DataRowVersion.Current);
			return dbCommand;
		}
		/// Author: khanhlq . Date: 2/8/2006.
		/// Modifier: khanhlq. Date:  2/8/2006.
		/// Liệt kê những gì đã chỉnh sửa:
		/// <summary>
		/// Deletes the wrapper.
		/// </summary>
		/// <returns></returns>
		private DbCommand DeleteWrapper()
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_DELETE);
			return dbCommand;
		}
		/// Author: khanhlq . Date: 2/8/2006.
		/// Modifier: khanhlq. Date:  2/8/2006.
		/// Liệt kê những gì đã chỉnh sửa:
		/// <summary>
		/// Does the update.
		/// </summary>
		/// <param name="ds">ds đã được thay đổi</param>
		/// <returns>result kết quả đúng hay sai</returns>
		public int DoUpdate(DataSet ds)
		{
			try
			{
				int result = db.UpdateDataSet(ds,Cls_PHANQUYEN_NHOM.TABLE_NAME,this.InsertWrapper(),this.UpdateWrapper(),this.DeleteWrapper(),UpdateBehavior.Standard);
				return result;
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		public DataSet ListByAll()
		{
			try
			{
				DataSet dsPhanQuyen = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_SELECTALL);
				db.LoadDataSet(dbCommand,dsPhanQuyen,Cls_PHANQUYEN_NHOM.TABLE_NAME);
				return dsPhanQuyen;
			}
			catch
			{
				return null;
			}
		}
		public DataSet ListByIDNhom(int ID_NhomNguoiDung)
		{
			try
			{
				DataSet dsPhanQuyen = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_SELECT_BY_IDNHOM);
				db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung",DbType.Int32,ID_NhomNguoiDung);
				db.LoadDataSet(dbCommand,dsPhanQuyen,Cls_PHANQUYEN_NHOM.TABLE_NAME);
				return dsPhanQuyen;
			}
			catch
			{
				return null;
			}
		}
		public string[] GetRoleByIDChucNang(int intID_Nguoi_Dung,string Ten_ChucNang)
		{
			try
			{
				DataSet dsPhanQuyen = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_SELECT_ROLE_BY_IDCHUCNANG);
				db.AddInParameter(dbCommand,"@ID_NguoiDung",DbType.Int32,intID_Nguoi_Dung);
				db.AddInParameter(dbCommand,"@Ten_ChucNang",DbType.String,Ten_ChucNang);
				db.LoadDataSet(dbCommand,dsPhanQuyen,Cls_PHANQUYEN_NHOM.TABLE_NAME);
				string[] arrRole = new string[dsPhanQuyen.Tables[0].Rows.Count];
				int i=0;
				foreach(DataRow dr in dsPhanQuyen.Tables[0].Rows)
				{
					arrRole[i] = dr["MaTruyCap"].ToString();
					i++;
				}
				return arrRole;
			}
			catch
			{
				return null;
			}
		}
		public Cls_PHANQUYEN_NHOM[] GetRoleByGroupID(int ID_NhomNguoiDung)
		{
			try
			{
				
				DataSet dsPhanQuyen = new DataSet();
				DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_SELECT_BY_IDNHOM);
				db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung",DbType.Int32,ID_NhomNguoiDung);
				db.LoadDataSet(dbCommand,dsPhanQuyen,Cls_PHANQUYEN_NHOM.TABLE_NAME);
				Cls_PHANQUYEN_NHOM[] ObjArrPhanQuyenNhom = new Cls_PHANQUYEN_NHOM[dsPhanQuyen.Tables[0].Rows.Count];
				int i = 0;
				foreach(DataRow dr in dsPhanQuyen.Tables[0].Rows)
				{
					Cls_PHANQUYEN_NHOM ObjPhanQuyen = new Cls_PHANQUYEN_NHOM();
					ObjPhanQuyen.ID_DS_ChucNang = Convert.ToInt32(dr["ID_DS_ChucNang"]);
					ObjPhanQuyen.ID_Nhom_NguoiDung = Convert.ToInt32(dr["ID_Nhom_NguoiDung"]);
					ObjPhanQuyen.ID_PhanQuyen_Nhom = Convert.ToInt32(dr["ID_PhanQuyen_Nhom"]);
					ObjPhanQuyen.MaTruyCap = dr["MaTruyCap"].ToString();
					ObjArrPhanQuyenNhom[i] = ObjPhanQuyen;
					i++;
				}
				return ObjArrPhanQuyenNhom;
			}
			catch
			{
				return null;
			}
		}
		public int Insert(int intID_Nhom_NguoiDung, int intID_DS_ChucNang, string strMaTruyCap)
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_INSERT);
			db.AddInParameter(dbCommand,"@ID_Nhom_NguoiDung", DbType.Int32,intID_Nhom_NguoiDung);
			db.AddInParameter(dbCommand,"@ID_DS_ChucNang", DbType.Int32,intID_DS_ChucNang);
			db.AddInParameter(dbCommand,"@MaTruyCap", DbType.String,strMaTruyCap);			
			return Convert.ToInt32(db.ExecuteScalar(dbCommand));		
		}	
		public int Update(int intID_PhanQuyen_Nhom, int intID_DS_ChucNang, string strMaTruyCap)
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_UPDATE);
			db.AddInParameter(dbCommand,"@ID_PhanQuyen_Nhom", DbType.Int32,intID_PhanQuyen_Nhom);
			db.AddInParameter(dbCommand,"@ID_DS_ChucNang", DbType.Int32,intID_DS_ChucNang);
			db.AddInParameter(dbCommand,"@MaTruyCap", DbType.String,strMaTruyCap);			
			return Convert.ToInt32(db.ExecuteScalar(dbCommand));
		}	
		public int Delete(int intID_PhanQuyen_Nhom)
		{
			DbCommand dbCommand = db.GetStoredProcCommand(Cls_PHANQUYEN_NHOM.ST_PHANQUYEN_NHOM_DELETE_BY_ID_PHANQUYEN_NHOM);
			db.AddInParameter(dbCommand,"@ID_PhanQuyen_Nhom", DbType.Int32,intID_PhanQuyen_Nhom);
			return Convert.ToInt32(db.ExecuteScalar(dbCommand));		
		}	
	}

}
